#ifndef __TEA5767_H__
#define __TEA5767_H__

#include "event.h"

#define TEA5767_I2C_SDA_IO 16          //I2C SDA GPIO Number   
#define TEA5767_I2C_SCL_IO 17          //I2C SCL GPIO Number    
#define TEA5767_I2C_POER_NUM 0         //FM 使用的 I2C 驱动号
#define TEA5767_I2C_FREQ 400000        //FM 对应 I2C 时钟  
#define TEA5767_ADDR 0x60              //FM I2C 器件地址    

#define TEA5767_MIN_FREQ    87500       //87.5Mhz
#define TEA5767_MAX_FREQ    108000      //108Mhz
#define TEA5767_CLK         32768

#define ACK_CHECK_EN   0x1

/* Write Byte 1 */
#define TEA5767_MUTE_ALL				(1<<7)
#define TEA5767_SEARCH_MODE				(1<<6)

/* Write Byte 2 */

/* Write Byte 3 */
#define TEA5767_SEARCH_UP				(1<<7)
#define TEA5767_SSL_MODE_0				(0<<5)
#define TEA5767_SSL_MODE_1				(1<<5)
#define TEA5767_SSL_MODE_2				(2<<5)
#define TEA5767_SSL_MODE_3				(3<<5)
#define TEA5767_HLSI_EN					(1<<4)
#define TEA5767_MONO_EN					(1<<3)
#define TEA5767_MUTE_RIGHT				(1<<2)
#define TEA5767_MUTE_LEFT				(1<<1)

/* Write Byte 4 */
#define TEA5767_STANDBY_MODE			(1<<6)
#define TEA5767_BL_JAPAN				(1<<5)
#define TEA5767_XTAL_32768				(1<<4)
#define TEA5767_SOFT_MUTE				(1<<3)
#define TEA5767_HCC_EN					(1<<2) /* High Cut Control */
#define TEA5767_SNC_EN					(1<<1) /* Stereo Noise Cancelling */
#define TEA5767_SI_OUT					(1<<0) /* SI = 1 then pin SWPORT1 is output for the ready flag */ 

/* Write Byte 5 */
#define TEA5767_PLLREF_6P5M_EN			(1<<7) /* 6.5 MHz reference frequency for the PLL is enabled */
#define TEA5767_DTC_7P5_US				(1<<6) /* de-emphasis time constant is 75 µs */

#define IF_COUNT_MIN					0x31   /* 32.768khz */
#define IF_COUNT_MAX					0x3e   /* 32.768khz */

enum fm_app_evt {
		FM_EVT_SEARCH = 0,
		FM_EVT_FREQ,
};

#define SAVE_FREQ_NUM					256

struct fm_evt_info {
	uint8_t event;
	uint32_t value;
};

esp_err_t tea5767_init(void);
void fm_start(uint8_t enable);
void fm_action(struct event_struct * evt);

#endif

